<?php

namespace App\Http\Controllers\Admin;

use App\Models\Guide;
use App\Models\GuidePage;

class GuideController extends Controller
{

    /*
     * @api get /course-api/a/guides 精彩绘本列表
     * @version 0.0.1
     * @group admin
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function index()
    {
        $guidesObj = Guide::get();
        $items = [];
        /** @var Guide $guideObj */
        foreach ($guidesObj as $guideObj) {
            $items[] = [
                'id' => $guideObj->getId(),
                'name' => $guideObj->getName(),
                'level' => $guideObj->getLevel(),
                'cover' => $guideObj->cover()->first(['id', 'name', 'uri', 'ext']),
                'isPublished' => $guideObj->getPublished()
            ];
        }
        return $this->sendSuccessJson(['items' => $items]);
    }

    /*
     * @api post /course-api/a/guides 新增精彩绘本
     * @version 0.0.1
     * @group admin
     *
     * @param body string name 名称
     * @param body string level 绘本级别，0容易，1困难
     * @param body string cover 封面
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function store()
    {
        $this->validate($this->request, [
            'name' => 'required|string',
            'cover' => 'required|integer',
            'level' => 'required|integer'
        ]);
        $name = $this->request->input('name');
        $level = $this->request->input('level');
        $cover = $this->request->input('cover');
        $guideObj = new Guide();
        $guideObj->setName($name);
        $guideObj->setLevel($level);
        $guideObj->setCover($cover);
        $guideObj->save();
        $data = ['id' => $guideObj->getId()];
        return $this->sendSuccessJson($data);
    }

    /*
     * @api put /course-api/a/guides/{guideId} 更新精彩绘本
     * @version 0.0.1
     * @group admin
     *
     * @param path int guideId 精彩绘本ID
     * @param body string name 名称
     * @param body string cover 封面
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function update($guideId)
    {
        $this->request->merge(['id' => $guideId]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            'name' => 'required|string',
            'cover' => 'required|integer',
            'level' => 'required|integer'
        ]);
        $name = $this->request->input('name');
        $level = $this->request->input('level');
        $cover = $this->request->input('cover');

        /** @var Guide $guideObj */
        $guideObj =  Guide::find($guideId);
        $guideObj->setName($name);
        $guideObj->setLevel($level);
        $guideObj->setCover($cover);
        $guideObj->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api get /course-api/a/guides/{guideId} 精彩绘本详情
     * @version 0.0.1
     * @group admin
     *
     * @param path int guideId 精彩绘本ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function show($guideId)
    {
        /** @var Guide $guideObj */
        $guideObj = Guide::find($guideId);
        $data = [
            'id' => $guideObj->getId(),
            'name' => $guideObj->getName(),
            'level' => $guideObj->getLevel(),
            'cover' => $guideObj->cover()->first(['id', 'name', 'uri']),
            'isPublished' => $guideObj->getPublished()
        ];
        return $this->sendSuccessJson($data);
    }

    /*
     * @api put /course-api/a/guides/{guideId}/pages-sequence 精彩绘本页面排序
     * @version 0.0.1
     * @group admin
     *
     * @param path int guideId 精彩绘本ID
     * @param path array pageIds 页面ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function sequence($guideId)
    {
        $pageIds = $this->request->input('pageIds');
        foreach ($pageIds as $index => $pageId) {
            /** @var GuidePage $guidePageObj */
            $guidePageObj = GuidePage::find($pageId);
            $guidePageObj->setSequence($index);
            $guidePageObj->save();
        }
        return $this->sendSuccessJson();
    }

    /*
     * @api delete /course-api/a/guides/{guideId} 删除精彩绘本
     * @version 0.0.1
     * @group admin
     *
     * @param path int guideId 精彩绘本ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function destroy($guideId)
    {
        Guide::destroy($guideId);
        return $this->sendSuccessJson();
    }


    /*
     * @api put /course-api/a/guides/{guideId}/publish 发布精彩绘本
     * @version 0.0.1
     * @group admin
     * @module guides
     *
     * @param path int guideId 精彩绘本ID
     * @param body int status 状态：0不发布，1发布
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     */
    public function publish($guideId)
    {
        $this->validate($this->request, [
            'status' => 'required'
        ]);
        $status = $this->request->input('status');
        /** @var Guide $guideObj */
        $guideObj = Guide::find($guideId);
        $guideObj->setPublished($status);
        if ($status == 1) {
            $guideObj->setMetadata($this->parsePage($guideId));
        }
        $guideObj->save();
        return $this->sendSuccessJson();
    }

    private function parsePage($guideId)
    {
        $metadata = [];
        /** @var Guide $guideObj */
        $guideObj = Guide::find($guideId);
        $metadata['title'] = $guideObj->getName();
        $metadata['cover'] = $guideObj->cover()->value('uri') . '?x-oss-process=style/guide_page';
        $pagesObj = GuidePage::where('guide_id', $guideId)->orderBy('sequence', 'asc')->orderBy('id', 'desc')->get();
        /** @var GuidePage $pageObj */
        foreach ($pagesObj as $pageObj) {
            $content = $pageObj->getContent();
            $metadata['pages'][] = [
                'image' => $content['attachment']['uri'] . '?x-oss-process=style/guide_page',
                'sentence' => [
                    'audio' => $content['sentences'][0]['audio']['uri'],
                    'text' => $content['sentences'][0]['sentence'],
                ]
            ];
        }
        return $metadata;
    }
}
